package com.stores.unitTests.admin;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Test;
import com.stores.unitTests.LocalDatabase;
import com.stores.v1.entity.Manufacturer;
import com.stores.v1.model.DatabaseCommandException;
import com.stores.v1.model.FilterOption;
import com.stores.v1.model.ManufacturerModel;
import com.stores.v1.toolset.DatabaseInitializationException;

public class ManufacturerFixture {

	@Test(expected = DatabaseCommandException.class)
	public void shouldNotCreateEmptyManufacturer()
			throws DatabaseInitializationException, DatabaseCommandException {
		LocalDatabase localDatabase = new LocalDatabase();
		Manufacturer mfg = new Manufacturer();
		ManufacturerModel model = new ManufacturerModel();
		model.setDatabase(localDatabase);
		model.addNew(mfg);
	}

	@Test
	public void shouldAddANewMfg() throws DatabaseInitializationException,
			DatabaseCommandException {
		LocalDatabase database = new LocalDatabase();
		Manufacturer mfg = new Manufacturer();
		mfg.setName("Lamcy Stores");
		mfg.setTagline("Lamcy Stores");
		mfg.setSmallLogoUrl("http://");
		mfg.setMediumLogoUrl("http://");
		mfg.setLargeLogoUrl("http://");
		ManufacturerModel model = new ManufacturerModel();
		model.setDatabase(database);
		int id = model.addNew(mfg);
		assertTrue(id > 1);

		mfg = new Manufacturer();
		mfg.setId((short) id);
		model.delete(mfg);
	}

	@Test
	public void shouldUpdateExistingMfg()
			throws DatabaseInitializationException, DatabaseCommandException {
		LocalDatabase database = new LocalDatabase();

		Manufacturer mfg = new Manufacturer();
		mfg.setName("Lamcy Stores");
		mfg.setTagline("Lamcy Stores");
		mfg.setSmallLogoUrl("http://");
		mfg.setMediumLogoUrl("http://");
		mfg.setLargeLogoUrl("http://");
		ManufacturerModel model = new ManufacturerModel();
		model.setDatabase(database);
		int id = model.addNew(mfg);
		assertTrue(id > 1);

		mfg = new Manufacturer();
		mfg.setId((short) id);
		mfg.setName("Lamcy Stores");
		mfg.setTagline("Lamcy Stores Updated");
		mfg.setSmallLogoUrl("http://");
		mfg.setMediumLogoUrl("http://");
		mfg.setLargeLogoUrl("http://");
		model.update(mfg);

		model.delete(mfg);
	}

	@Test
	public void shouldConditionallySelect()
			throws DatabaseInitializationException, DatabaseCommandException {
		LocalDatabase database = new LocalDatabase();
		Manufacturer mfg = new Manufacturer();
		mfg.setName("Lamcy Stores");
		mfg.setTagline("Lamcy Stores");
		mfg.setSmallLogoUrl("http://");
		mfg.setMediumLogoUrl("http://");
		mfg.setLargeLogoUrl("http://");
		ManufacturerModel model = new ManufacturerModel();
		model.setDatabase(database);
		int first = model.addNew(mfg);
		mfg.setName("d2");
		int second = model.addNew(mfg);
		mfg.setName("2d");
		int third = model.addNew(mfg);
		mfg.setName("11");
		int fourth = model.addNew(mfg);
		mfg.setName("dd");
		int fifth = model.addNew(mfg);
		mfg.setName("33");
		int sixth = model.addNew(mfg);

		FilterOption option = new FilterOption();
		option.setOrderByDescending(true);
		option.setOrderByDescendingColumn("name");

		List<Manufacturer> results = model.retrieve(option);
		assertEquals(6, results.size());
		
		option.setWhereClause("name like 'd%'");
		results = model.retrieve(option);
		assertEquals(2, results.size());
		
		option.setWhereClause(null);
		
		option.setTake(3);
		results = model.retrieve(option);
		assertEquals(3, results.size());
		
		option.setSkip(2);
		option.setTake(3);
		results = model.retrieve(option);
		assertEquals(3, results.size());
		
		option.setSkip(4);
		option.setTake(3);
		results = model.retrieve(option);
		assertEquals(2, results.size());
		
		mfg.setId((short) first);
		model.delete(mfg);
		mfg.setId((short) second);
		model.delete(mfg);
		mfg.setId((short) third);
		model.delete(mfg);
		mfg.setId((short) fourth);
		model.delete(mfg);
		mfg.setId((short) fifth);
		model.delete(mfg);
		mfg.setId((short) sixth);
		model.delete(mfg);
	}
}
